package com.cduestc.animation;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.cduestc.weatheractivity.HomePage;
import com.cduestc.weatheractivity.R;

public class OpenAnimation extends AppCompatActivity {
    private int state = 5;
    private TextView showMsg,tvMsg;
    private ProgressBar pBar;
    private Button skip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open_animation);

        showMsg = this.findViewById(R.id.showMsg);
        pBar = this.findViewById(R.id.pBar);
        skip = this.findViewById(R.id.skip);
        tvMsg = this.findViewById(R.id.tvMsg);
        String str1 = "";

        showMsg.animate().translationY(-1400).setDuration(2700).setStartDelay(0);

        CountDownTimer cdt = new CountDownTimer(5*1000,1000) {
            @Override
            public void onTick(long l) {
                skip.setText("跳过" + (l/1000));
                tvMsg.setText(str1);
            }

            @Override
            public void onFinish() {
                Intent intent2 = new Intent(OpenAnimation.this, HomePage.class);
                startActivity(intent2);
                finish();
            }
        };

        cdt.start();

        skip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cdt.cancel();
                Intent intent1 = new Intent(OpenAnimation.this, HomePage.class);
                startActivity(intent1);
                finish();
            }
        });

        Handler hand2 = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 0x111) {
                    pBar.setProgress(state);
                }
            }
        };

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (state >= 0) {
                    state = countNum();
                    Message mg = new Message();
                    mg.what = 0x111;
                    hand2.sendMessage(mg);
                }
            }
        }).start();
    }

    public int countNum() {
        state--;
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return state;
    }
}